package com.zaizai.aop;

import com.zaizai.domain.AjaxResult;
import com.zaizai.domain.Log;
import com.zaizai.domain.Role;
import com.zaizai.service.ILogService;
import com.zaizai.service.IRoleService;
import com.zaizai.utils.BaseContext;
import com.zaizai.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

@Slf4j
@Component
@Aspect
public class LogAspect {
    @Autowired
    private ILogService logService;
    @Autowired
    private IRoleService roleService;

    //开始时间
    private static long beginTime;

    @Around("@annotation(autoLog)")
    public Object recordLog(ProceedingJoinPoint joinPoint, AutoLog autoLog) throws Throwable {
        //操作名称
        String name = autoLog.value();

        //操作人
        String username = "";
        Long userId = BaseContext.getCurrentId();
        Role role = roleService.selectRoleById(userId);
        if(role != null) {
           username = role.getUserName();
        }

        //操作ip
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr();

        long begin = System.currentTimeMillis();
        //执行方法
        AjaxResult result = (AjaxResult) joinPoint.proceed();
        long end = System.currentTimeMillis();
        //操作耗时
        long costTime = end - begin;

        log.info("操作人：" + username + "，操作名称：" + name + "，操作耗时：" + costTime + "ms" + "，操作ip：" + ip);

        Log log = new Log(null, name, username, ip, costTime, new Date());
        logService.insertLog(log);

        return result;
    }



    @Before("@annotation(loginLog)")
    public void BeforeLoginLog(JoinPoint joinPoint, LoginLog loginLog) throws Throwable {
        beginTime = System.currentTimeMillis();
    }
    @AfterReturning("@annotation(loginLog)")
    public void AfterLoginLog(JoinPoint joinPoint, LoginLog loginLog) throws Throwable {
        //操作名称
        String name = loginLog.value();

        //操作人
        String username = "";
        Long userId = BaseContext.getCurrentId();
        Role role = roleService.selectRoleById(userId);
        if(role != null) {
            username = role.getUserName();
        }

        //操作ip
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getRemoteAddr();

        long endTime = System.currentTimeMillis();
        //操作耗时
        long costTime = endTime - beginTime;

        log.info("操作人：" + username + "，操作名称：" + name + "，操作耗时：" + costTime + "ms" + "，操作ip：" + ip);

        Log log = new Log(null, name, username, ip, costTime, new Date());
        logService.insertLog(log);
    }
}
